# -*- coding:utf-8 -*-

import os, json, types

"""
从json文件读取数据到列表中
"""


class DouyinJson(object):

    def __init__(self):
        self.data = []

    def json_get(self, json, l_key, default):
        ret = json
        for k in l_key:
            if type(k) is int:
                if k < 0:
                    return default
                if not (type(ret) is list):
                    return default
                if len(ret) <= k:
                    return default
            elif type(k) is str:
                if not (type(ret) is dict):
                    return default
                if not k in ret.keys():
                    return default
            else:
                return default
            ret = ret[k]

        return ret


    def format_json(self, file_list = []):
        '''
        格式化文件，过滤广告和无用的数据，返回列表
        :param file_list:
        :return:
        '''
        for file in file_list:
            print(file)
            # 判断文件是否存在
            if os.path.isfile(file) == False:
                print('error: ', file, '文件不存在')
                continue
            # 判断文件是否可读
            if os.access(file, os.R_OK) == False:
                print('error: ', file, '文件没有读取权限')
                continue

            json_data = []

            try:
                with open(file) as content:
                    json_data = json.load(content)
            except:
                print('error: ', file, '文件读取失败')
                continue

            if 'aweme_list' not in json_data.keys():
                print('error: ', file, ',aweme_list 值不存在')
                continue

            for tmp in json_data['aweme_list']:
                # 去掉广告
                if 'raw_ad_data' in tmp.keys():
                    continue
                title = self.json_get(tmp, ['cha_list', 0, 'cha_name'], '')
                if title == '':
                    title = self.json_get(tmp, ['desc'], '')
                if title == '':
                    title = '抖音小视频'

                # 如果没有获取到描述内容则把标题改为和
                desc = self.json_get(tmp, ['cha_list', 0, 'desc'], '')
                if desc == '':
                    desc = self.json_get(tmp, ['desc'], '')

                if desc == '':
                    desc = "无尽有趣小视频！"
                try:
                    self.data.append({
                        'video_id': tmp['aweme_id'],
                        'user_id': tmp['author_user_id'],
                        'title': title,
                        'desc': desc,
                        'upload_count': tmp['statistics']['digg_count'] + tmp['statistics']['share_count'] * 5 +
                                        tmp['statistics']['comment_count'] * 5,
                        'digg_count': tmp['statistics']['digg_count'],
                        'share_count': tmp['statistics']['share_count'],
                        'comment_count': tmp['statistics']['comment_count'],
                        'pick_up': 0,
                        'download_url': tmp['video']['play_addr']['url_list'][0],
                        'is_download': 0,
                        'is_upload': 0
                    })
                except:
                    continue

        return self.data


if __name__ == '__main__':
    douyin_json = DouyinJson()
    json_file = ['D:\\python\\downloadDouUpdateYou\\file\\27_.json', ]
    douyin_json.format_json(json_file)
    print(douyin_json.data)
